/**
 * 
 */
package com.tomagency.cyberlab.server.rpc;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.tomagency.cyberlab.client.rpc.ScannerService;
import com.tomagency.cyberlab.server.config.FileSystemConfig;
import com.tomagency.cyberlab.server.imports.TaopixFtpBackgroundScanner;
import com.tomagency.cyberlab.server.util.LoggingUtil;

import de.gammadata.gwt.modelle.ResultObject;
import de.gammadata.gwt.modelle.ResultObjectImpl;

import org.apache.log4j.Logger;

/**
 * @author gfr
 *
 */
@Singleton
public class ScannerControlServlet extends RemoteServiceServlet implements ScannerService{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	@Inject 
	private LoggingUtil loggingUtil;
	@Inject 
	private FileSystemConfig fsystem;
	@Inject
	TaopixFtpBackgroundScanner taopixScanner;
	
	Thread taopixScannerThread;
	
	Logger logger;
	
	public ScannerControlServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param delegate
	 */
	public ScannerControlServlet(Object delegate) {
		super(delegate);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void init(){
		try {
			logger = loggingUtil.getScannerLogger(ScannerControlServlet.class.getName());
			taopixScanner.setName("Taopix Ftp Scanner");
			taopixScanner.setLogger(logger);
			if (!taopixScanner.init()){
				logger.error("Fehler beim Initialisieren TaopixFtpBackgroundScanner!!");
			}
			
			taopixScannerThread = new Thread(taopixScanner);
			logger.info("Starte TaopixFtpBackgroundScanner -------------------------");
			taopixScannerThread.start();
		} catch (Exception e) {
			logger.error("Fehler beim Initialisieren TaopixFtpBackgroundScanner!!",e);
		}
	}

	@Override
	public String getTaopixScannerStatus() {
		if (taopixScanner==null){
			return ("Kein Taopix-Scanner vorhanden");
		}
		return taopixScanner.getInfo();
	}
	
	
	@Override
	public ResultObject changeSannerRunModus(boolean start) {
		
		if (!start){
			if(taopixScanner!=null&&taopixScannerThread!=null){
			taopixScanner.stopAndTerminate();
			taopixScannerThread.interrupt();
			logger.info("Taopix-Scanner gestoppt");
			return new  ResultObjectImpl("Taopix-Scanner gestoppt",true);
			}
		}
		else{
			if (taopixScanner==null){
				init();
				logger.info("Taopix-Scanner neu gestartet");
				return new  ResultObjectImpl("Taopix-Scanner neu gestartet",true);
			}
			else if (taopixScannerThread!=null&&taopixScannerThread.isAlive()){
				logger.info("Taopix-Scanner läuft noch");
				return new  ResultObjectImpl("Taopix-Scanner läuft noch",false);
			}
			else{
				init();
				logger.info("Taopix-Scanner neu gestartet");
				return new  ResultObjectImpl("Taopix-Scanner neu gestartet",true);
			}
		}
		logger.info("Taopix-Scanner neu gestartet");
		return new  ResultObjectImpl("Taopix-Scanner neu gestartet",true);
	}

	@Override
	public ResultObjectImpl doImmediateScan() {
		if (taopixScanner==null){
			logger.info("Kein Taopix-Scanner vorhanden");
			return new  ResultObjectImpl("Kein Taopix-Scanner vorhanden",false);
		}
		else if (taopixScannerThread!=null&&taopixScannerThread.isAlive()){
			taopixScanner.doImmediateScan();
			taopixScannerThread.interrupt();
			logger.info("immediateScan initiert, Scanner-Status: "+taopixScanner.getInfo());
			return new  ResultObjectImpl("immediateScan initiert, Scanner-Status: "+taopixScanner.getInfo(),true);
		}
		else{
			logger.info("Taopix-Scanner läuft nicht");
			return new  ResultObjectImpl("Taopix-Scanner läuft nicht",false);
		}
		
	}

}
